package com.example.demo.Service.Impl;
import com.example.demo.Dao.BookDao;
import com.example.demo.Dao.CategoryDao;
import com.example.demo.Entity.Book;
import com.example.demo.Entity.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.Service.BookService;

import javax.persistence.criteria.CriteriaBuilder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookDao bookDao;

    @Autowired
    private CategoryDao categoryDao;

    @Override
    public ArrayList<Book> selectBookByCategory(String category) {
        return bookDao.selectBookByCategory(category);
    }
    @Override
    public ArrayList<Book> selectBookByKeyWord(String keyword){
        return bookDao.selectBookByKeyWord(keyword);
    }

    @Override
    public  Book selectBookByISBN(String isbn){
        return bookDao.selectBookByISBN(isbn);
    }

    @Override
    public ArrayList<Book> selectAll(){
        return bookDao.selectAll();
    }

    @Override
    public void createNewBook(Book book) {
        bookDao.createNewBook(book);
    }

    @Override
    public boolean deleteBook(String isbn) {
        Book book;
        book = bookDao.selectBookByISBN(isbn);
        if(book == null){
            return false;
        }
        bookDao.deleteBook(isbn);
        return true;
    }

    @Override
    public String selectCategoryName(String categoryId) {
        return categoryDao.selectCategoryNameById(categoryId).getCategoryName();
    }

    @Override
    public boolean editBookTitle(String isbn, String title) {
        bookDao.editBookTitle(isbn, title);
        return true;
    }

    @Override
    public boolean editBookAuthor(String isbn, String author) {
        bookDao.editBookAuthor(isbn, author);
        return true;
    }

    @Override
    public boolean editBookCategoryID(String isbn, String temcategory) {
        try{
            int category = Integer.parseInt(temcategory);
            if(category < 0 ){
                return false;
            }
            bookDao.editBookCategoryID(isbn, category);
        } catch (Exception e){
            return false;
        }
        return true;
    }

    @Override
    public boolean editBookPages(String isbn, String tempages) {
        try{
            int pages = Integer.parseInt(tempages);
            if(pages < 0 ){
                return false;
            }
            bookDao.editBookPages(isbn, pages);
        } catch (Exception e){
            return false;
        }
        return true;
    }

    @Override
    public boolean editBookWords(String isbn, String temwords) {
        try{
            int words = Integer.parseInt(temwords);
            if(words < 0 ){
                return false;
            }
            bookDao.editBookWords(isbn, words);
        } catch (Exception e){
            return false;
        }
        return true;
    }

    @Override
    public boolean editBookPublisher(String isbn, String publisher) {
        bookDao.editBookPublisher(isbn, publisher);
        return true;
    }

    @Override
    public boolean editBookPrice(String isbn, String temprice) {
        try{
            float price = Float.parseFloat(temprice);
            if(price < 0 ){
                return false;
            }
            bookDao.editBookPrice(isbn, price);
        } catch (Exception e){
            return false;
        }
        return true;
    }

    @Override
    public boolean editBookTotalCount(String isbn, String temtotalcount) {
        try{
            int totalcount = Integer.parseInt(temtotalcount);
            if(totalcount < 0 ){
                return false;
            }
            bookDao.editBookTotalCount(isbn, totalcount);
        } catch (Exception e){
            return false;
        }
        return true;
    }

    @Override
    public boolean editBookIntroduction(String isbn, String introduction) {
        bookDao.editBookIntroduction(isbn, introduction);
        return true;
    }

    @Override
    public boolean editBookActionDate(String isbn, String temactiondate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try{
            Date actiondate = sdf.parse(temactiondate);
            bookDao.editBookActionDate(isbn, actiondate);
        } catch (ParseException e){
            return false;
        }
        return true;
    }

    @Override
    public boolean editBookPictureUrl(String isbn, String pictureurl) {
        bookDao.editBookPictureUrl(isbn, pictureurl);
        return true;
    }

    @Override
    public boolean editBookPictureBase(String isbn, String picturebase) {
       bookDao.editBookPictureBase(isbn, picturebase);
        return true;
    }
}
